package ggze.esp.controller;

import ggze.esp.dao.inter.FspViewSqlDao;
import ggze.esp.dao.inter.MPSDao;
import ggze.esp.entity.FspViewSql;
import ggze.loader.Storage;
import ggze.tool.ReturnBody;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
public class BspController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;
    @Autowired
    private FspViewSqlDao fspViewSqlDao;
    @Autowired
    private MPSDao mpsDao;

    @RequestMapping(value = "/bsp", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public ReturnBody BSP(@RequestBody JSONObject jsonObject) {
        ReturnBody returnBody = new ReturnBody();
        try {
            //验证静态token
            String APPID = jsonObject.getString("APPID");
            if (!APPID.equals(Storage.getObj().getConfig().get("APPID"))) {
                returnBody.setTitle("请输入正确的APPID");
                returnBody.setCode("0");
                return returnBody;
            }
            String uuid = jsonObject.getString("uuid");
            FspViewSql fspViewSql = fspViewSqlDao.isSelect("uuid", uuid);
            String sql = fspViewSql.getF_SQL();
            if (fspViewSql.getF_STATUS().equals("1")) {
                returnBody.setTitle("已被禁用！");
                returnBody.setCode("1");
            } else {
                if (fspViewSql.getF_BEAN().equals("1")) {
                    if (sql.indexOf("where") < 0 && sql.indexOf("WHERE") < 0) {
                        sql += " where ";
                    }
                    sql += this.packSqlBane(jsonObject);
                    if (fspViewSql.getF_IS_ORDER().equals("0")) {
                        sql += " order by F_METHOD_TIME desc";
                    }
                    List<Map<String, String>> list = mpsDao.commonSql(sql);
                    returnBody.setCount(list.size());
                    list = DISTINCTS(list);
                    if (fspViewSql.getF_IS_ORDER().equals("0")) {
                        int begin = Integer.parseInt(jsonObject.getString("page"));
                        int end = Integer.parseInt(jsonObject.getString("limit"));
                        begin = (begin - 1) * end;
                        sql += " limit " + begin + ", " + end;
                        list = mpsDao.commonSql(sql);
                    }
                    returnBody.setCode("0");
                    returnBody.setData(list);
                } else {
                    //查询
                    if (fspViewSql.getF_TYPE().equals("4")) {
                        List<Map<String, String>> list = mpsDao.commonSql(sql);
                        returnBody.setCode("0");
                        returnBody.setData(list);
                    } else if (fspViewSql.getF_TYPE().equals("5")) {
                        //列名
                        returnBody.setCode("0");
                        returnBody.setData(fspViewSql.getF_SQL());
                    } else {
                        int i = mpsDao.commonSqlUp(sql);
                        returnBody.setCode("0");
                        returnBody.setData(i);
                    }
                }

            }
        } catch (Exception e) {
            returnBody.setCode("1");
        }
        return returnBody;
    }

    //bean映射
    private String packSqlBane(JSONObject jsonObject) {
        StringBuilder where = new StringBuilder();
        try {
            where.append(" 1=1 ");
            List<Map<String, String>> whereMAP = (List<Map<String, String>>) jsonObject.getJSONArray("common");
            //ZDNAME,ZDVAL,LIKE
            for (Map<String, String> ZD : whereMAP) {
                if (ZD.get("LIKE").equals("1")) {
                    where.append(" AND " + ZD.get("ZDNAME") + " = '" + ZD.get("ZDVAL") + "' ");
                } else {
                    where.append(" AND " + ZD.get("ZDNAME") + " like '%" + ZD.get("ZDVAL") + "%' ");
                }
            }
        } catch (Exception e) {
            return "";
        }
        return where.toString();
    }

    private List<Map<String, String>> DISTINCTS(List<Map<String, String>> list) {
        List<Map<String, String>> list2  = new LinkedList<>();
        for(int i = 0; i <list.size(); i++){
            Map<String, String> ro = getZD(list2, "UUID",list.get(i).get("UUID"));
            if(ro!=null){
                if(ro.get("LAY_CHECKED").equals("false")){
                    list2.set(getIndex(list2, "UUID",list.get(i).get("UUID")),list.get(i));
                }
            }else{
                list2.add(list.get(i));
            }
        }
        return list2;
    }
    private Map<String,String> getZD(List<Map<String,String>> rote,String key,String values){
        for(Map<String,String> ro:rote){
            for(String keys:ro.keySet()){
                if(keys.equals(key)&&ro.get(keys).equals(values)){
                    return ro;
                }
            }
        }
        return null;
    }
    private int getIndex(List<Map<String,String>> rote,String key,String values){
        for(int i=0;i<rote.size();i++){
            Map<String,String> ro=rote.get(i);
            for(String keys:ro.keySet()){
                if(keys.equals(key)&&ro.get(keys).equals(values)){
                    return i;
                }
            }
        }
        return -1;
    }
}
